<?php

/**
 * Subclass for representing a row from the 'newsletter' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Newsletter extends BaseNewsletter
{
	public function getStatusName()
	{
		return $name = MyConstants::getStatusName($this->getStatusId());
	}
    public static function subscriberCount()
    {
        $c = new Criteria();
        $c->add(SubscriberPeer::STATUS_ID, MyConstants::STATUS_APPROVED);
        $subscribers = SubscriberPeer::doSelect($c);
        $num = count($subscribers);
        return $num;
    }
    /**
     * 
     * returns true, if newsltter has requested object
     * @param int $objType
     * @param int $objId
     */
    public function hasObj($objType, $objId)
    {
    	$arr = json_decode($this->getItemsJson());
    	$has = false;
    	for( $i = 0; $i < count($arr); $i++ )
    	{
    		if( ($arr[$i][0] == $objType) && ($arr[$i][1] == $objId) )
    		  return true;
    	}
    	return $has;
    }
    
    /**
     * 
     * returns NewsletterObject by object type and id (returns null if not found)
     * throws exception for unexpected object type
     * @param int $objType
     * @param int $objId
     * @param string $lng
     */
    public static function newsletterFactory($objType, $objId, $lng = 'lt')
    {
    	$obj = null;
    	switch ($objType) {
		    case MyConstants::TYPE_NEWS:
		        $news = NewsPeer::RetrieveByPk($objId);
		        if($news)
		        {
		        	$news->setCulture($lng);
		        	$obj = new NewsletterObject();
		        	$obj->setTitle($news->getName());
		        	$obj->setText($news->getSummary());
		        	$img = Files::getFirst($objId
                                        , $objType
                                        , MyConstants::UNDEFINED_ID);
		        	$obj->setImageUname($img ? $img->getUname() : null);
		        }
		        break;
		    case MyConstants::TYPE_TEXTMODULE:
    	       $text = TextModulePeer::RetrieveByPk($objId);
                if($text)
                {
                	$text->setCulture($lng);
                    $obj = new NewsletterObject();
                    $obj->setTitle($text->getName());
                    $obj->setText($text->getText());
                    $img = Files::getFirst($objId
                                        , $objType
                                        , MyConstants::UNDEFINED_ID);
                    $obj->setImageUname($img ? $img->getUname() : null);
                }
		        break;
		    case MyConstants::TYPE_PRODUCT:
		    	/* @var $prod Product */
		    	$prod = ProductPeer::RetrieveByPk($objId);
                if($prod)
                {
                	$prod->setCulture($lng);
                    $obj = new NewsletterObject();
                    $obj->setTitle($prod->getName());
                    $obj->setText($prod->getText());
                    $img = Files::getFirst($objId
                                        , $objType
                                        , MyConstants::UNDEFINED_ID);
                    $obj->setImageUname($img ? $img->getUname() : null);
                }
		        break;
		    default:
		    	throw new Exception('Object type unexpected.');
    	}
    	return $obj;
    }
}
